package com.cn.kehong.service;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cn.kehong.api.ICurveWeightService;
import com.cn.kehong.dao.CurveWeightMapper;
import com.cn.kehong.domain.CurveWeight;
import com.cn.kehong.domain.CurveWeightExample;
import com.cn.kehong.utils.CollectionsUtil;
import com.cn.kehong.utils.CommonUtil;
import com.cn.kehong.utils.Constants;
import com.cn.kehong.utils.QueryForm;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@EnableScheduling
@Service
public class CurveWeightServiceImpl implements ICurveWeightService {

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    private CurveWeightMapper curveWeightMapper;

    @Override
    public List<CurveWeight> getCurveWeights(QueryForm queryForm) throws Exception {
        String factoryName = queryForm.getFactoryName();
        String type = queryForm.getType();
        String guanCode = queryForm.getCode();
        if(StringUtils.isEmpty(factoryName)||StringUtils.isEmpty(type)||StringUtils.isEmpty(guanCode)){
            return Lists.newArrayList();
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date now = new Date();
        long preHour = now.getTime() - 60*60*1000;

        String preHourDay = simpleDateFormat.format(preHour);
        String nowDay = simpleDateFormat.format(now);
        CurveWeightExample example = new CurveWeightExample();
        example.createCriteria().andStatusEqualTo(Constants.NORMAL_STATUS).andFactoryNameEqualTo(factoryName)
                .andCurveTypeEqualTo(type).andCurveGuanCodeEqualTo(guanCode).andCurveTimeBetween(preHourDay,nowDay);
        example.setOrderByClause("curve_time asc");
        List<CurveWeight> curveWeights = curveWeightMapper.selectByExample(example);
        return curveWeights;
    }

    @Override
    public String addCurveWeight(CurveWeight curveWeight) throws Exception {
        return null;
    }

    @Override
    public String updateCurveWeight(CurveWeight curveWeight) throws Exception {

        return null;
    }

    @Scheduled(cron = "0 0/5 * * * ?")
    public void actionCurveWeightRedis() throws Exception {
        long roomSize = redisTemplate.opsForList().size("161t");
        List<String> allRedisList = Lists.newArrayList();
        for(int i =0;i <roomSize;i++){
            Object value = redisTemplate.opsForList().rightPop("161t");
            if(null != value){
                allRedisList.add(value.toString());
            }
        }
        long roomSize1 = redisTemplate.opsForList().size("162t");
        for(int i =0;i <roomSize1;i++){
            Object value = redisTemplate.opsForList().rightPop("162t");
            if(null != value){
                allRedisList.add(value.toString());
            }
        }

        List<CurveWeight> curveWeights = Lists.newArrayList();
        Map<String,JSONObject> curveWeightMap = Maps.newHashMap();
        for (int i = 0; i < allRedisList.size(); ++i) {
            try {
                JSONObject roomObject = JSON.parseObject(allRedisList.get(i));
                String factoryName = roomObject.getString("Name");
                String timestamp = roomObject.getString("TimeStamp");
                if(StringUtils.equals(factoryName,"161t")){
                    factoryName = "16-1";
                }else if(StringUtils.equals(factoryName,"162t")){
                    factoryName = "16-2";
                }

                if(curveWeightMap.containsKey(factoryName)){
                    JSONObject existJson = curveWeightMap.get(factoryName);
                    if(existJson.getString("TimeStamp").compareTo(timestamp) < 0){
                        curveWeightMap.put(factoryName,roomObject);
                    }
                }else {
                    curveWeightMap.put(factoryName,roomObject);
                }

            } catch (Exception e) {

            }
        }
        for(String key:curveWeightMap.keySet()){
            JSONObject jsonObject = curveWeightMap.get(key);
            String timestamp = jsonObject.getString("TimeStamp");
            String chumolwe = jsonObject.getString("chumolwe");
            String chumo2we = jsonObject.getString("chumo2we");
            String chumo3we = jsonObject.getString("chumo3we");
            String zz1we = jsonObject.getString("zz1we");
            String zz2we = jsonObject.getString("zz2we");
            String zz3we = jsonObject.getString("zz3we");
            String ximolwe = jsonObject.getString("ximolwe");
            String ximo2we = jsonObject.getString("ximo2we");
            String ximo3we = jsonObject.getString("ximo3we");
            String ximo4we = jsonObject.getString("ximo4we");
            String ximo5we = jsonObject.getString("ximo5we");
            String ximo6we = jsonObject.getString("ximo6we");
            String ximo7we = jsonObject.getString("ximo7we");
            String ximo8we = jsonObject.getString("ximo8we");
            String ximo9we = jsonObject.getString("ximo9we");
            String chuci1we = jsonObject.getString("chuci1we");
            String chuci2we = jsonObject.getString("chuci2we");
            String chuci3we = jsonObject.getString("chuci3we");
            String chengp1we = jsonObject.getString("chengp1we");
            String chengp2we = jsonObject.getString("chengp2we");
            String chengp3we = jsonObject.getString("chengp3we");
            String chengp4we = jsonObject.getString("chengp4we");
            String chengp5we = jsonObject.getString("chengp5we");
            String chengp6we = jsonObject.getString("chengp6we");
            String timeStr[] = timestamp.split(" ")[0].split("/");
            String year =timeStr[0];
            String moth =timeStr[1];
            String day =timeStr[2];
            this.addCurveWeight(chumolwe,"1","1",key,year,moth,day,timestamp,curveWeights);
            this.addCurveWeight(chumo2we,"1","2",key,year,moth,day,timestamp,curveWeights);
            this.addCurveWeight(chumo3we,"1","3",key,year,moth,day,timestamp,curveWeights);
            this.addCurveWeight(zz1we,"1","4",key,year,moth,day,timestamp,curveWeights);
            this.addCurveWeight(zz2we,"1","5",key,year,moth,day,timestamp,curveWeights);
            this.addCurveWeight(zz3we,"1","6",key,year,moth,day,timestamp,curveWeights);
            this.addCurveWeight(ximolwe,"2","1",key,year,moth,day,timestamp,curveWeights);
            this.addCurveWeight(ximo2we,"2","2",key,year,moth,day,timestamp,curveWeights);
            this.addCurveWeight(ximo3we,"2","3",key,year,moth,day,timestamp,curveWeights);
            this.addCurveWeight(ximo4we,"2","4",key,year,moth,day,timestamp,curveWeights);
            this.addCurveWeight(ximo5we,"2","5",key,year,moth,day,timestamp,curveWeights);
            this.addCurveWeight(ximo6we,"2","6",key,year,moth,day,timestamp,curveWeights);
            this.addCurveWeight(ximo7we,"2","7",key,year,moth,day,timestamp,curveWeights);
            this.addCurveWeight(ximo8we,"2","8",key,year,moth,day,timestamp,curveWeights);
            this.addCurveWeight(ximo9we,"2","9",key,year,moth,day,timestamp,curveWeights);
            this.addCurveWeight(chuci1we,"3","1",key,year,moth,day,timestamp,curveWeights);
            this.addCurveWeight(chuci2we,"3","2",key,year,moth,day,timestamp,curveWeights);
            this.addCurveWeight(chuci3we,"3","3",key,year,moth,day,timestamp,curveWeights);
            this.addCurveWeight(chengp1we,"4","1",key,year,moth,day,timestamp,curveWeights);
            this.addCurveWeight(chengp2we,"4","2",key,year,moth,day,timestamp,curveWeights);
            this.addCurveWeight(chengp3we,"4","3",key,year,moth,day,timestamp,curveWeights);
            this.addCurveWeight(chengp4we,"4","4",key,year,moth,day,timestamp,curveWeights);
            this.addCurveWeight(chengp5we,"4","5",key,year,moth,day,timestamp,curveWeights);
            this.addCurveWeight(chengp6we,"4","6",key,year,moth,day,timestamp,curveWeights);
        }
        if(CollectionsUtil.isNotEmpty(curveWeights)){
            curveWeightMapper.batchInsertCurveWeights(curveWeights);
        }
    }

    private void addCurveWeight(String value,String type,String guanCode,String factoryName,String year,String moth,String day,String timestamp,List<CurveWeight> curveWeights){
        CurveWeight curveWeight = new CurveWeight();
        curveWeight.setId(CommonUtil.generateRandomNum("curve-"));
        curveWeight.setCurveType(type);
        curveWeight.setFactoryName(factoryName);
        curveWeight.setCurveWeight(value);
        curveWeight.setCurveGuanCode(guanCode);
        curveWeight.setCurveYear(year);
        curveWeight.setCurveMoth(moth);
        curveWeight.setCurveDay(day);
        curveWeight.setCurveTime(timestamp);
        curveWeights.add(curveWeight);
    }

}
